package com.javaspeak.designpatterns.go4.behavioural.memento;

/**
 * This class describes document editing functionality such as saving the
 * document text, font family and font size.
 * <p>
 * This interface extends the Originator interface which has additional methods
 * like:
 * <pre>
 * {@code
 * public Memento getMemento();
 * public void restoreFromMemento( Memento memento );
 * }
 * </pre>
 * The Document interface is implemented by the Originator.
 * When the getMemento() method is called on the Document the Document returns
 * a snapshot of its state in a DocumentMemento object.
 * <p>
 * When the restoreFromMemento(..) is called on the Document the Document will
 * roll back its state to the state encapsulated in the DocumentMemento object.
 *
 * @author John Dickerson
 */
public class DocumentImpl implements Document {

    private String documentText;
    private String fontFamily;
    private int fontSize;

    /**
     * Inner class used to create a Memento saving the current state of the
     * document text.  Note that the final DocumentMemento has a private
     * constructor and no setters so its state cannot be modified after it has
     * been created.
     */
    final class DocumentMemento implements Memento {

        final private String documentMemento;

        private DocumentMemento( String document ){

            documentMemento = new String(document);
        }

        private String getDocumentText(){

            return documentMemento;
        }
    }



// START Document interface methods ============================================

    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.memento.
     *      Document#saveDocumentText(java.lang.String)
     */
    @Override
    public void saveDocumentText( String documentText ) {

        this.documentText = documentText;
    }


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.memento.
     *      Document#getDocument()
     */
    @Override
    public String getDocumentText() {

        return this.documentText;
    }


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.memento.
     *      Document#saveFontFamily(java.lang.String)
     */
    @Override
    public void saveFontFamily(String fontFamily) {

        this.fontFamily = fontFamily;
    }


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.memento.
     *      Document#getFontFamily()
     */
    @Override
    public String getFontFamily() {

        return fontFamily;
    }


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.memento.
     *      Document#saveFontSize(int)
     */
    @Override
    public void saveFontSize(int fontSize) {

        this.fontSize = fontSize;
    }


    /* (non-Javadoc)
     * @see com.javaspeak.designpatterns.go4.behavioural.memento.
     *      Document#getFontSize()
     */
    @Override
    public int getFontSize() {

        return fontSize;
    }

// END Document interface methods ==============================================


// START Caretaker interface methods ===========================================

    @Override
    public Memento getMemento() {

        return new DocumentMemento( documentText );
    }


    @Override
    public void restoreFromMemento( Memento memento ) {

        DocumentMemento documentMemento = ( DocumentMemento )memento;
        this.documentText =  documentMemento.getDocumentText();
    }

// END Caretaker interface methods =============================================
}
